////////////////////////////////////////////////////////////////////////////////
// Filename: light.ps
////////////////////////////////////////////////////////////////////////////////


/////////////
// GLOBALS //
/////////////

Texture2D shaderTexture;
SamplerState sampleType;

cbuffer LightBuffer
{
	float4 ambientColor;
    float4 diffuseColor;
    float3 lightDirection;
	float specularPower;
	float4 specularColor;
};

//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
    float4 position : SV_POSITION;
	float2 tex : TEXCOORD0;
	float3 normal : NORMAL;
	float3 viewDirection : TEXCOORD1;
};

////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 LightPixelShader(PixelInputType input) : SV_TARGET
{
	float4 textureColor;
	float3 lightDir;
	float lightIntensity;
	float4 color;
	float3 reflection;
	float specular;

	textureColor = shaderTexture.Sample(sampleType, input.tex);

	color = ambientColor;
	// Init specular color
	specular = float4(0.0f, 0.0f, 0.0f, 0.0f);

	// Invert the light direction for calculations.
    lightDir = -lightDirection;

    // Calculate the amount of light on this pixel.
    lightIntensity = saturate(dot(input.normal, lightDir));

	if(lightIntensity > 0.0f)
	{
		color += (diffuseColor * lightIntensity);

		// Saturate ambient and diffuse
		color = saturate(color);

		// Calculate reflection
		reflection = normalize(2 * lightIntensity * input.normal - lightDir);

		// Determine amount of specular light
		specular = pow(saturate(dot(reflection, input.viewDirection)), specularPower);
	}

	// Multiply texture pixeland input color to get textured result
	color *= textureColor;

	return saturate(color + specular);
}
